package com.hbnu.dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class FileTransferDao extends JDBC{
    //检查md5是否存在
    public int checkMd5(String md5) {
        try(PreparedStatement preparedStatement=getPreparedStatement(
                "select file_id from files where file_md5= ?;"
        )){
            // 设置 PreparedStatement 参数
            preparedStatement.setString(1, md5);
            ResultSet resultSet = preparedStatement.executeQuery();
            if(resultSet.next())
                return  resultSet.getInt(1);
            else {
               return -1;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    //保存服务器的数据到数据库
    public int insertFileData(String uid,long size,String md5,String path) {
        try(PreparedStatement preparedStatement=getPreparedStatement0(
                "insert into files(user_id,file_size,file_md5,file_path,created_time) \n" +
                        "values(?,?,?,?,now())"
        )){
            // 设置 PreparedStatement 参数
            preparedStatement.setString(1, uid);
            preparedStatement.setLong(2, size);
            preparedStatement.setString(3, md5);
            preparedStatement.setString(4, path);
            // 执行插入操作
            if (preparedStatement.executeUpdate() > 0) {
                // 获取生成的主键
                try (ResultSet generatedKeys = preparedStatement.getGeneratedKeys()) {
                    if (generatedKeys.next()) {
                        // 获取生成的主键值（假设是 int 类型）
                        return generatedKeys.getInt(1);
                    }
                    throw new RuntimeException("获取主键异常！");
                }
            }else {
                throw  new RuntimeException("保存异常！");
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    //保存到用户数据库
    public int saveUserFile(String uid,String fname,int pid,int fileId) {
        try(PreparedStatement preparedStatement=getPreparedStatement0(
                " insert into files_folders(user_id,fname,pid,created_time,file_id,is_file)\n" +
                        "values(?,?,?,now(),?,1)"
        )){
            // 设置 PreparedStatement 参数
            preparedStatement.setString(1, uid);
            preparedStatement.setString(2, fname);
            preparedStatement.setInt(3, pid);
            preparedStatement.setInt(4, fileId);
            if (preparedStatement.executeUpdate() > 0) {
                // 获取生成的主键
                try (ResultSet generatedKeys = preparedStatement.getGeneratedKeys()) {
                    if (generatedKeys.next()) {
                        // 获取生成的主键值（假设是 int 类型）
                        return generatedKeys.getInt(1);
                    }
                    throw new RuntimeException("获取主键异常！");
                }
            }else  throw new RuntimeException("数据库保存异常");
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
